package com.atguigu.msmservice.service.impl;

import com.atguigu.msmservice.service.MsmService;
import com.atguigu.msmservice.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @create: 2022/10/7
 * @Description:
 * @FileName: MsmServiceImpl
 * @自定义内容：
 */
@Service
public class MsmServiceImpl implements MsmService {
    @Autowired
    private JavaMailSender javaMailSender;

    //生成四位验证码
    @Override
    public String getCode() {
//        int random = (int) (Math.random() * 1000000);
//        System.out.println(random);
//        String code = String.format("%06d", random);
//        System.out.println(code);
        String code = RandomUtil.getSixBitRandom();
        System.out.println(code);
        return code;
    }

    //发送邮箱验证码方法
    @Override
    public boolean sendEmail(String email, String code) {
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setSubject("谷粒学院登录验证码");
            simpleMailMessage.setText("尊敬："+email+"您的注册校验验证码为："+code+"有效期为5分钟");
            simpleMailMessage.setTo(email);
            simpleMailMessage.setFrom("2929408642@qq.com");
            javaMailSender.send(simpleMailMessage);
            return true;
        }catch (Exception e){
            e.printStackTrace();
//            throw new GuliException(20001,"邮箱验证码发送失败");
            return false;
        }

    }
}
